<?php
    //父母操作类
    class ParentAction extends Action{
    
    public function _initialize() {
        $pid=Session::get('p_id');
	if(empty($pid)){
      $this->redirect('Public/login');
    //  $this->error("请先登陆");  $pid
    }
	}
        public function index(){
            $this->display("Tpl/default/Public/parent/maincontent.html");
        }
        //编辑用户的信息
        public function editInfo(){
            $map['p_id']=$_POST['p_id'];
            $Model=M('Parent');
            $List=$Model->where($map)->findAll();
            echo "调用了";
            return json_encode($List);
        }
        //响应编辑动作
        public function doEdit(){
            //得到页面参数的相关信息
            $data['p_id']=1;
            $data['nick']=$_POST['nick'];
            $data['sex']=$_POST['sex'];
            $data['birthday']=$_POST['birthday'];
            $data['birthAdd']=$_POST['birthAdd'];
            $data['currAdd']=$_POST['currAdd'];
            $data['job']=$_POST['job'];
            $Model=M('Parent');
            //编辑
            $as=$Model->data($data)->save();
            echo $as;
                   
        }
    /**
     * 更改父亲界面信息
     * @author sudi
     * @time 2012年3月12日
     */
     function change_info()
     {
         $pid = Session::get('p_id');
         $condition['p_id'] = $pid;
         $Pdao = M('Parent');
         $parent = $Pdao->where($condition)->find();
         $name = $parent['nick'];
         $pcond['pid'] = $pid;
         $List = M('Parentinfo')->where($pcond)->find();
         $this->assign('parent',$parent);
         $this->assign('parentname',$name);
         $this->assign('parentid',$pid); 
         $this->assign('list',$List);
         $this->display('Tpl/default/Parent/parentinfo.html');  
    } 
        
       /**
     * @author sudi
     * @2012年3月23日
     * 保存修改界面
     */
     
    function saveInfo()
    {
         $pid = Session::get('p_id');
         if($pid==0)                                             //文件名为空
         {
         echo"<script>alert('请确认是否登录，或者长时间离开浏览器!'); 
         window.history.back();</script>";
         }  
         $data['pid']= $pid;
         $data['province'] = $_REQUEST['province'];
      
         $data['school'] = $_REQUEST['school'];
      
         $data['company'] = $_REQUEST['company'];
         $data['address'] = $_REQUEST['address'];
         $condition['pid'] = $pid;
         $list = M('Parentinfo')->where($condition)->find();
         if($list==null)
         {
            $last = M('Parentinfo')->add($data);
         }
         else{
            $last = M('Parentinfo')->where($condition)->save($data);
         }
         $Public = A('Public');
         $Public->toparent();
    }   
        
        
       /**
     * @author sudi
     * @time 2012年3月12日
     */
    function addShare()
    {
        $content = $_POST['content'];
  //      $this->ajaxReturn($content,'',1,'JSON');
        $data['content'] = $content;
        $data['pid'] = Session::get('p_id');
        $data['ctime'] = time();
        $dao = D('pshare');
        $last = $dao->add($data);
        $condition['id'] = $last;
        $List = $dao->where($condition)->find();  
        $List['ctime'] = date('Y-m-d H:i:s',$List['ctime']);   
        $this->ajaxReturn($List,'',1,'JSON');       
    }
        
     /**
      * 更改图像
      * @author sudi
      */
      public function headPhoto()
      {
         $photo=$_FILES['photo'];
      //获得各个图片信息
           $name=$photo['name'];
           $tmp_name=$photo["tmp_name"];
           $type=$photo["type"];
           $size=$photo["size"];
           $err = $photo["error"];
           $path = "/home/Tpl/default/Public/upload/head/".$name;
           $uploadfile = SITE_PATH.$path;
           $maxsize=800*1024;          //最大允许上许文件大小
            if($name=="")                                             //文件名为空
             {
             echo"<script>alert('请先选择要上传的图片文件!'); 
             window.history.back();</script>";
             }
            if($type!="image/pjpeg" && $type!="image/jpeg" && $type!="image/gif")//文件类型不在指定范围
            {
             echo"<script>alert('上传文件只可以是JPEG或GIF类型的!');
             window.history.back();</script>";
             exit;
            }
            if($size>$maxsize)                                       //超过规定大小
            {
             echo"<script>alert('上传文件大小不能超过800K! ');window.history.back();</script>";
             exit;
            }
     
        if($err>0){
            switch($err) 
            { 
            case 1: echo "<script>alert('文件大小超过服务器限制! ');window.history.back();</script>"; 
            break; 
            case 2: echo "<script>alert('上传文件太大小! ');window.history.back();</script>"; 
            break; 
            case 3: echo "<script>alert('文件至加载一半，请重新加载! ');window.history.back();</script>"; 
            break; 
            case 4: echo "<script>alert('文件加载失败! ');window.history.back();</script>"; 
            break; 
            } 
          }          
         $pid = Session::get('p_id');        
         if($pid==0)                                             //文件名为空
         {
         echo"<script>alert('请确认是否登录，或者长时间离开浏览器!'); 
         window.history.back();</script>";
         }
         $condition['p_id'] = $pid; 
         $data['picture'] = $name;
         $lastid = M('Parent')->where($condition)->save($data);
         if($lastid>0){
          if(move_uploaded_file($tmp_name,$uploadfile))
            {
            $Parent = A('Public');
            $Parent->toparent();
            }
          else if (copy($tmp_name,$uploadfile))
            {
             $Parent = A('Public');
            $Parent->toparent();
            }
          else
            return false; 
        }     
        else
            return false;  
      }
        
        //进入编辑页面
        public function addAvatar(){
            
          $this->display("Tpl/default/Public/avatar/avatar.html");
        }
        //保存图片
        public function saveAvatar(){
            $data['avatar']=$_POST['avatar'];
            $data['p_id']=1;
            $Model=M('Parent');
            $res=$Model->data($data)->save();
            $this->ajaxReturn("",$res,1,"JSON");
        }
        
        public function dispAvatar(){
            
        }
        
    }

?>